﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Maticsoft.DAL
{
	/// <summary>
	/// 数据访问类:TeachingClass
	/// </summary>
	public partial class TeachingClass
	{
		public TeachingClass()
		{}
		#region  Method


		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string TC_Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from TeachingClass");
			strSql.Append(" where TC_Id='"+TC_Id+"'");
			return DbHelperSQL.Exists(strSql.ToString());
		}

        //教师时间被占用
        public bool TeacherNoTime(string T_Id,string Date_Day,string Date_Time)
         {
           StringBuilder strSql = new StringBuilder();
           strSql.Append("select count(1) from TeachingClass");
           strSql.Append(" where T_Id='" + T_Id + "' and TC_Date_Time='" + Date_Time + "' and TC_Date_Day='" + Date_Day +"'");
           return DbHelperSQL.Exists(strSql.ToString());
        }

        //教室被占用
        public bool ClassNoFree(string CL_Id, string Date_Day, string Date_Time)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from TeachingClass");
            strSql.Append(" where CL_Id='" + CL_Id + "' and TC_Date_Time='" + Date_Time + "' and TC_Date_Day='" + Date_Day + "'");
            return DbHelperSQL.Exists(strSql.ToString());
        
        }

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Maticsoft.Model.TeachingClass model)
		{
			StringBuilder strSql=new StringBuilder();
			StringBuilder strSql1=new StringBuilder();
			StringBuilder strSql2=new StringBuilder();
			if (model.TC_Id != null)
			{
				strSql1.Append("TC_Id,");
				strSql2.Append("'"+model.TC_Id+"',");
			}
			if (model.C_Id != null)
			{
				strSql1.Append("C_Id,");
				strSql2.Append("'"+model.C_Id+"',");
			}
			if (model.T_Id != null)
			{
				strSql1.Append("T_Id,");
				strSql2.Append("'"+model.T_Id+"',");
			}
			if (model.CL_Id != null)
			{
				strSql1.Append("CL_Id,");
				strSql2.Append("'"+model.CL_Id+"',");
			}
			if (model.TC_Name != null)
			{
				strSql1.Append("TC_Name,");
				strSql2.Append("'"+model.TC_Name+"',");
			}
			if (model.TC_Department != null)
			{
				strSql1.Append("TC_Department,");
				strSql2.Append("'"+model.TC_Department+"',");
			}
			if (model.TC_Date_Day != null)
			{
				strSql1.Append("TC_Date_Day,");
				strSql2.Append("'"+model.TC_Date_Day+"',");
			}
			if (model.TC_Date_Time != null)
			{
				strSql1.Append("TC_Date_Time,");
				strSql2.Append("'"+model.TC_Date_Time+"',");
			}
			strSql.Append("insert into TeachingClass(");
			strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
			strSql.Append(")");
			strSql.Append(" values (");
			strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
			strSql.Append(")");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Maticsoft.Model.TeachingClass model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update TeachingClass set ");
			if (model.TC_Name != null)
			{
				strSql.Append("TC_Name='"+model.TC_Name+"',");
			}
			if (model.TC_Department != null)
			{
				strSql.Append("TC_Department='"+model.TC_Department+"',");
			}
			else
			{
				strSql.Append("TC_Department= null ,");
			}
			if (model.TC_Date_Day != null)
			{
				strSql.Append("TC_Date_Day='"+model.TC_Date_Day+"',");
			}
			if (model.TC_Date_Time != null)
			{
				strSql.Append("TC_Date_Time='"+model.TC_Date_Time+"',");
			}
			int n = strSql.ToString().LastIndexOf(",");
			strSql.Remove(n, 1);
			strSql.Append(" where TC_Id='"+ model.TC_Id+"' and C_Id='"+ model.C_Id+"' and T_Id='"+ model.T_Id+"' and CL_Id='"+ model.CL_Id+"' ");
			int rowsAffected=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string TC_Id,string C_Id,string T_Id,string CL_Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from TeachingClass ");
			strSql.Append(" where TC_Id='"+TC_Id+"' and C_Id='"+C_Id+"' and T_Id='"+T_Id+"' and CL_Id='"+CL_Id+"' " );
			int rowsAffected=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
        public Maticsoft.Model.TeachingClass GetModel(string TC_Id, string C_Id, string T_Id, string CL_Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1  ");
			strSql.Append(" TC_Id,C_Id,T_Id,CL_Id,TC_Name,TC_Department,TC_Date_Day,TC_Date_Time ");
			strSql.Append(" from TeachingClass ");
            strSql.Append(" where TC_Id='" + TC_Id + "' and C_Id='"+CL_Id+"' and T_Id='"+T_Id+"' and CL_Id='"+CL_Id+"'");
			Maticsoft.Model.TeachingClass model=new Maticsoft.Model.TeachingClass();
			DataSet ds=DbHelperSQL.Query(strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.TeachingClass DataRowToModel(DataRow row)
		{
			Maticsoft.Model.TeachingClass model=new Maticsoft.Model.TeachingClass();
			if (row != null)
			{
				if(row["TC_Id"]!=null)
				{
					model.TC_Id=row["TC_Id"].ToString();
				}
				if(row["C_Id"]!=null)
				{
					model.C_Id=row["C_Id"].ToString();
				}
				if(row["T_Id"]!=null)
				{
					model.T_Id=row["T_Id"].ToString();
				}
				if(row["CL_Id"]!=null)
				{
					model.CL_Id=row["CL_Id"].ToString();
				}
				if(row["TC_Name"]!=null)
				{
					model.TC_Name=row["TC_Name"].ToString();
				}
				if(row["TC_Department"]!=null)
				{
					model.TC_Department=row["TC_Department"].ToString();
				}
				if(row["TC_Date_Day"]!=null)
				{
					model.TC_Date_Day=row["TC_Date_Day"].ToString();
				}
				if(row["TC_Date_Time"]!=null)
				{
					model.TC_Date_Time=row["TC_Date_Time"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select TC_Id,C_Id,Teacher.T_Id,T_Name,CL_Id,TC_Name,TC_Department,TC_Date_Day,TC_Date_Time ");
            strSql.Append(" FROM TeachingClass,Teacher");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
        /// 
        public DataSet GetAllInformation()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            strSql.Append(" TC_Id,C_Id,T_Id,CL_Id,TC_Name,TC_Department,TC_Date_Day,TC_Date_Time ");
            strSql.Append(" FROM TeachingClass ");
            return DbHelperSQL.Query(strSql.ToString());
        }
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" TC_Id,C_Id,T_Id,CL_Id,TC_Name,TC_Department,TC_Date_Day,TC_Date_Time ");
			strSql.Append(" FROM TeachingClass ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM TeachingClass ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.CL_Id desc");
			}
			strSql.Append(")AS Row, T.*  from TeachingClass T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		*/

		#endregion  Method
		#region  MethodEx

		#endregion  MethodEx
	}
}

